import rclpy 
from rclpy.node  import Node 
from autopator_interfaces.srv  import SpeechText  # 注意检查包名是否正确 
import espeakng 
 
class Speaker(Node):
    def __init__(self, node_name):
        super().__init__(node_name)  # 修正父类初始化 
        self.speech_service_  = self.create_service( 
            SpeechText, 
            'speech_text',
            self.speech_text_callback)   # 修正回调函数名
        self.speaker_  = espeakng.Speaker()
        self.speaker_.voice  = 'zh'
 
    def speech_text_callback(self, request, response):  # 正确的方法名
        self.get_logger().info(f' 正准备朗读{request.text}') 
        self.speaker_.say(request.text) 
        self.speaker_.wait() 
        response.result  = True 
        return response 
 
def main(args=None):
    rclpy.init(args=args)   # 正确初始化 
    node = Speaker('speaker')
    rclpy.spin(node) 
    rclpy.shutdown() 
 
if __name__ == '__main__':
    main()